package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.bean.BeanStatusInfo;
import com.landeb.shop.huo.bean.BeanUserData;
import com.landeb.shop.huo.httpRequest.UserConfirmationRequest;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PasswordFindActivity extends BaseActivity implements View.OnClickListener {

    private Button btnConfirm, btnSendSmsCode;
    private LinearLayout btnBack;
    private EditText edPhoneNum, edSmsCode, edNewPassword, edConfirmPassword;

    private String mUrl;
    private Retrofit mRetrofit;
    private UserConfirmationRequest mRequest;

    private Subscription mTimer;
    private int mTimeCount = 0;

    public static void toAct(Context context) {
        Intent intent = new Intent(context, PasswordFindActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_find);


        OkHttpClient client = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(UserConfirmationRequest.class);


        initView();
        initData();
        initEvent();

    }

    private void initEvent() {

        btnBack.setOnClickListener(this);
        btnSendSmsCode.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);

    }

    private void initData() {

    }

    private void initView() {

        btnBack = (LinearLayout) findViewById(R.id.act_findPassword_btn_back);
        btnSendSmsCode = (Button) findViewById(R.id.act_findPassword_btn_sendSmsCode);
        btnConfirm = (Button) findViewById(R.id.act_findPassword_btn_resetPassword);

        edPhoneNum = (EditText) findViewById(R.id.act_findPassword_ed_phoneNum);
        edSmsCode = (EditText) findViewById(R.id.act_findPassword_ed_smsCode);
        edNewPassword = (EditText) findViewById(R.id.act_findPassword_ed_NewPassword);
        edConfirmPassword = (EditText) findViewById(R.id.act_findPassword_ed_confirmPassword);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.act_findPassword_btn_back:
                // TODO: 2016/11/16 返回键
                onBackPressed();
                break;

            case R.id.act_findPassword_btn_sendSmsCode:
                // TODO: 2016/11/16 发送验证码

                if (edPhoneNum.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordFindActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

                btnSendSmsCode.setEnabled(false);
                mRequest.sendSmsCode(edPhoneNum.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BeanUserData>() {
                            @Override
                            public void onCompleted() {
                                Log.e("sendSmsCode", "successed");

                            }

                            @Override
                            public void onError(Throwable e) {
                                Log.e("sendSmsCode", "ERROR");
                            }

                            @Override
                            public void onNext(BeanUserData beanUserData) {
                                //若返回验证码正常  60s才能请求  若返回失败 把请求按钮设为enable
                                Toast.makeText(PasswordFindActivity.this, beanUserData.getStatus() + beanUserData.getInfo(), Toast.LENGTH_SHORT).show();
                                if (beanUserData.getStatus() == 1) {
                                    countTime4Btn();
                                } else {
                                    btnSendSmsCode.setEnabled(true);
                                }

                            }
                        });

                break;
            case R.id.act_findPassword_btn_resetPassword:
                // TODO: 2016/11/16 重置密码

                if (edNewPassword.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordFindActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (edConfirmPassword.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordFindActivity.this, "请确认密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (edPhoneNum.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordFindActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!edNewPassword.getText().toString().equals(edConfirmPassword.getText().toString())) {
                    Toast.makeText(PasswordFindActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (edSmsCode.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordFindActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }

//                Toast.makeText(SignUpActivity.this, edPhoneNum.getText().toString(), Toast.LENGTH_SHORT).show();
                mRequest.getPassword(edPhoneNum.getText().toString(), edSmsCode.getText().toString(), edNewPassword.getText().toString(), edConfirmPassword.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BeanStatusInfo>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BeanStatusInfo beanStatusInfo) {
                                Toast.makeText(PasswordFindActivity.this, beanStatusInfo.getInfo(), Toast.LENGTH_SHORT).show();

                            }
                        });

                break;
        }


    }

    private void countTime4Btn() {

        mTimeCount = 0;
        mTimer = rx.Observable.interval(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onCompleted() {
                        Log.e("BtnTimer", "onCompleted:xxxxxxxxxxxxxx ");
                    }

                    @Override
                    public void onError(Throwable e) {

                        Log.e("BtnTimer", "onError:xxxxxxxxxxxxxx ");


                    }

                    @Override
                    public void onNext(Long aLong) {


                        mTimeCount++;
                        if (mTimeCount <= 60) {
                            btnSendSmsCode.setText("等待(" + (60 - mTimeCount) + " s)");
                        } else {
                            btnSendSmsCode.setText("获取验证码");
                            btnSendSmsCode.setEnabled(true);
                        }

                        Log.e("BtnTimer", "xxxxxxxxxxxxxx" + mTimeCount);


                    }


                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mTimer != null && mTimer.isUnsubscribed()) {
            mTimer.unsubscribe();
        }
    }
}
